//
//  ***** COMPLETE *****
//
//  VDIP.c
//  ECGVer2
//
//  Created by FRANCIS PAPINEAU on 12-01-01.
//  Copyright 2012 carleton university. All rights reserved.
//
//  A couple of USB Interface functions that print to the USB
//  directly. The follwoing file system is put in place to
//  arganize the files into a system.
//
//  FileSystem:
//      -> Folder#session (From EEPROM)
//          -> TempData.csv 
//          -> Vitals.csv
//          -> Others.csv
//          -> System.txt
//          -> Acknowledge.txt

/* --------------------------------------------------------- */

void closeAllFiles(void){
    
    // Close all files for writting
    
    // command for USB FTDI: CLF<sp>NAME<cr>
    
    Serial2.print("CLF TempData");
    Serial2.print(SessionID);
    Serial2.print(".CSV");
    Serial2.print(13, BYTE);
    delay(100);
    
    Serial2.print("CLF Vitals");
    Serial2.print(SessionID);
    Serial2.print(".CSV");
    Serial2.print(13, BYTE);
    delay(100);
    
    Serial2.print("CLF Others");
    Serial2.print(SessionID);
    Serial2.print(".CSV");
    Serial2.print(13, BYTE);
    delay(100);
    
    Serial2.print("CLF System");
    Serial2.print(SessionID);
    Serial2.print(".txt");
    Serial2.print(13, BYTE);
    delay(100);
    
    Serial2.print("CLF Acknowledge");
    Serial2.print(SessionID);
    Serial2.print(".txt");
    Serial2.print(13, BYTE);
    delay(100);
    
    goToLine(1);
    LCDprint("Files Closed");
    delay(1000);
}

/* --------------------------------------------------------- */

void putInSuspendMode(void){
    
    // Put in suspend mode to save power
    Serial2.print("SUD");
    Serial2.print(13, BYTE);
    delay(100);
}

/* --------------------------------------------------------- */

void openNewFiles(void){
    
    // open new files to store values in. Using the EEPROM
    // to print the titles of the files, and the folders
    
    // make dir;
    // ‘MKD’<sp><name><cr>
    
    Serial2.print("MKD ");
    Serial2.print(SessionFolder);
    Serial2.print(SessionID);
    Serial2.print(13, BYTE);
    delay(100);
    
    Serial2.print("CD ");
    Serial2.print(SessionFolder);
    Serial2.print(SessionID);
    Serial2.print(13, BYTE);
    delay(100);
    
    // command for USB FTDI: OTW<sp>NAME<cr>
    // Creating files
    
    Serial2.print("OPW TempData");
    Serial2.print(SessionID);
    Serial2.print(".CSV");
    Serial2.print(13, BYTE);
    delay(100);
    
    Serial2.print("OPW Vitals");
    Serial2.print(SessionID);
    Serial2.print(".CSV");
    Serial2.print(13, BYTE);
    delay(100);
    
    Serial2.print("OPW Others");
    Serial2.print(SessionID);
    Serial2.print(".CSV");
    Serial2.print(13, BYTE);
    delay(100);
    
    Serial2.print("OPW System");
    Serial2.print(SessionID);
    Serial2.print(".txt");
    Serial2.print(13, BYTE);
    delay(100);
    
    Serial2.print("OPW Acknowledge");
    Serial2.print(SessionID);
    Serial2.print(".txt");
    Serial2.print(13, BYTE);
    delay(100);
    
    // sending through USB the new File System
    goToLine(1);
    LCDprint("New files opened");
    delay(1000);
    
}

/* --------------------------------------------------------- */

void deleteFiles(void){
    
    // delete the specific files written in the root directory
    
    // command for FTDI : DLF<sp>NAME<cr>
    // deleting files
    
    Serial2.print("DLF TempData");
    Serial2.print(SessionID);
    Serial2.print(".CSV");
    Serial2.print(13, BYTE);
    delay(100);
    
    Serial2.print("DLF Vitals");
    Serial2.print(SessionID);
    Serial2.print(".CSV");
    Serial2.print(13, BYTE);
    delay(100);
    
    Serial2.print("DLF Others");
    Serial2.print(SessionID);
    Serial2.print(".CSV");
    Serial2.print(13, BYTE);
    delay(100);
    
    Serial2.print("DLF System");
    Serial2.print(SessionID);
    Serial2.print(".txt");
    Serial2.print(13, BYTE);
    delay(100);
    
    Serial2.print("DLF Acknowledge");
    Serial2.print(SessionID);
    Serial2.print(".txt");
    Serial2.print(13, BYTE);
    delay(100);
    
    // go back to root
    Serial2.print("CD ..");
    Serial2.print(13, BYTE);
    delay(100);
    
    // delete dir
    Serial2.print("DLD "+SessionFolder);
    Serial2.print(13, BYTE);
    delay(100);
    
    // sending through USB the new File System
    goToLine(1);
    LCDprint("Files/Dir Deleted");
    delay(1000);
    
}

/* --------------------------------------------------------- */

void setToASCIIMode(void){
    
    // Sets the VDIP to ASCII mode
    
    Serial2.print("IPA");  
    Serial2.print(13, BYTE);
    delay(100);
}

/* --------------------------------------------------------- */

void processVdipBuffer(){
    
    // echo what is going on on the screen
    byte incomingBYTE;
    
    while(Serial2.available()>0){
        // read the RX buffer
        incomingBYTE = Serial2.read();
        // if the incoming value is 13 printline
        if(incomingBYTE == 0x13) {
        }
        LCDprintChar((char)incomingBYTE);
    }
}

/* --------------------------------------------------------- */

void printHeaders(void){
    
    // print the headers for the CSV files
    
    // 1. open the file, 
    // 2. send the print command
    // 3. send the length 
    // 4. send data to be written
    // 5. close the file
    
    // ========== Temperature File =============
    
    //1.
    Serial2.print("OPW TempData");
    Serial2.print(SessionID);
    Serial2.print(".CSV");
    Serial2.print(13, BYTE);
    delay(100);
    
    //2.
    Serial2.print("WRF ");
    
    //3.
    Serial2.print(TempHeader.length());
    Serial2.print(13, BYTE);
    
    //4.
    Serial2.print(TempHeader);
    Serial2.print(13, BYTE);
    
    //6.
    Serial2.print("CLF TempData");
    Serial2.print(SessionID);
    Serial2.print(".CSV");
    Serial2.print(13, BYTE);
    delay(100);
    
    // ========== Vital Signs File =============
    
    //1.
    Serial2.print("OPW Vitals");
    Serial2.print(SessionID);
    Serial2.print(".CSV");
    Serial2.print(13, BYTE);
    delay(100);
    
    //2.
    Serial2.print("WRF ");
    
    //3.
    Serial2.print(VitalsHeader.length());
    Serial2.print(13, BYTE);
    
    //4.
    Serial2.print(VitalsHeader);
    Serial2.print(13, BYTE);
    
    //5.
    Serial2.print("CLF Vitals");
    Serial2.print(SessionID);
    Serial2.print(".CSV");
    Serial2.print(13, BYTE);
    delay(100);
    
    // ========== Other Parameters File =============
    
    //1.
    Serial2.print("OPW Others");
    Serial2.print(SessionID);
    Serial2.print(".CSV");
    Serial2.print(13, BYTE);
    delay(100);
    
    //2.
    Serial2.print("WRF ");
    
    //3.
    Serial2.print(OthersHeader.length());
    Serial2.print(13, BYTE);
    
    //4.
    Serial2.print(OthersHeader);
    Serial2.print(13, BYTE);
    
    //5.
    Serial2.print("CLF Others");
    Serial2.print(SessionID);
    Serial2.print(".CSV");
    Serial2.print(13, BYTE);
    delay(100);
    
    goToLine(1);
    LCDprint("Headers Printed!");
    delay(1000);
    
}

/* --------------------------------------------------------- */

void printACK_Sys_files(void){
    
    // ========== Print ACK FILE =============
    
    // print an acknoledgment that the system is OK
    // and running with no issues.
    
    // OPEN ACK FILE
    Serial2.print("OPW Acknowledge.txt");
    Serial2.print(13, BYTE);
    delay(100);
    
    // SEND print COMMAND
    Serial2.print("WRF ");
    
    // SEND THE LENGTH OF THE STRING
    
    Serial2.print(ACK.length());
    Serial2.print(13, BYTE);
    delay(100);
    
    // SEND DATA
    Serial2.print(ACK);
    Serial2.print(13, BYTE);
    delay(100);
    
    // CLOSE FILE
    Serial2.print("CLF Acknowledge.txt");
    Serial2.print(13, BYTE);
    delay(100);
    
    // ========== Print SYSTEM INFO File =============
    
    // Print the system file with:
    //  -> Fimware Ver.
    //  -> Drive Info
    //  -> Drive Info Extended
    //  -> Free Mem.
    //  -> Current DIR
    
    Serial2.print("OPW System.txt");
    Serial2.print(13, BYTE);
    delay(100);
    
    // ========== frimware version =============
    
    //get
    Serial2.print("FWV");
    Serial2.print(13, BYTE);
    
    String buffer, printString;
    while(Serial2.available() > 0){
        buffer = buffer + Serial2.read();
    }
    
    printString = "This is the system file. It is used to allow for\n"
    "Debugging purposes. It shows the follwing:\n"
    "\t-> firmware version: "+ buffer + "\n\n";
    
    Serial2.print("WRF ");
    Serial2.print(printString.length());
    Serial2.print(13, BYTE);
    
    // print
    Serial2.print(printString);
    Serial2.print(13, BYTE);
    delay(100);
    
    // ========== free space =============
    
    //get
    Serial2.print("FSE");
    Serial2.print(13, BYTE);
    
    buffer = NULL;
    printString =NULL;
    
    while(Serial2.available() > 0){
        buffer = buffer + Serial2.read();
    }
    
    printString = "\t-> free space on USB: " + buffer + "\n\n";
    
    Serial2.print("WRF ");
    Serial2.print(printString.length());
    Serial2.print(13, BYTE);
    
    // print
    Serial2.print(printString);
    Serial2.print(13, BYTE);
    delay(100);
    
    // ========== current directory =============
    
    // get
    Serial2.print("DIR");
    Serial2.print(13, BYTE);
    
    buffer = NULL;
    printString =NULL;
    
    while(Serial2.available() > 0){
        buffer = buffer + Serial2.read();
    }
    
    printString = "\t-> Current Directory: " + buffer + "\n\n";
    
    Serial2.print("WRF ");
    Serial2.print(printString.length());
    Serial2.print(13, BYTE);
    
    // print
    Serial2.print(printString);
    Serial2.print(13, BYTE);
    delay(100);
    
    // ========== device information =============
    
    //get
    Serial2.print("IDDE");
    Serial2.print(13, BYTE);
    
    buffer = NULL;
    printString =NULL;
    
    while(Serial2.available() > 0){
        buffer = buffer + Serial2.read();
    }
    
    printString = "\t-> Additionnal Info: \n" + buffer + "\n\n" +
    "*********************************************\n" +
    "This file is for debugging purposes only and\n" +
    "should not be taken as the end all of all.\n" +
    "*********************************************\n" +
    "This startup process took approximetly: " + millis() + "\n";
    
    Serial2.print("WRF ");
    Serial2.print(printString.length());
    Serial2.print(13, BYTE);
    
    // print
    Serial2.print(printString);
    Serial2.print(13, BYTE);
    delay(100);
    
    // LCD ACK of printing done!
    goToLine(1);
    LCDprint("System/ACK printed!!!");
    delay(1000);
    
}

/* --------------------------------------------------------- */

void FillUSBBuffers(void){
    
    // fill buffers with values
    
    // generate a BuffersFull flag when the buffers are full
    // and need to be written to the USB Stick...
    // WE are using the BLOCK memory mode this way less
    // time is taken to print to the files
    
    if(logEntryTemp.length() == 140 || logEntryVitals.length() == 260||
       logEntryOthers.length() == 140){
        BuffersFull = 1;
    }
    
    if(BuffersFull == 0){
        
        // Creating the Temperature record
        logEntryTemp += millis();
        logEntryTemp += ",";
        logEntryTemp += temp;
        logEntryTemp += ",";
        logEntryTemp += AmbientTemp;
        logEntryTemp += ",";
        logEntryTemp += (AmbientTemp - temp);
        logEntryTemp += "\n";
        
        //creating the other system data record
        logEntryOthers += millis();
        logEntryOthers += ",";
        logEntryOthers += tempBat;
        logEntryOthers += ",";
        logEntryOthers += tempECGBat;
        logEntryOthers += ",";
        logEntryOthers += tempLight;
        logEntryOthers += "\n";
        
        //creating Vital data record
        logEntryVitals += millis();
        logEntryVitals += ",";
        logEntryVitals += tempECG;
        logEntryVitals += ",";
        logEntryVitals += ECGPulseTemp ;
        logEntryVitals += ",";
        logEntryVitals += tempRed;
        logEntryVitals += ",";
        logEntryVitals += tempIr;
        logEntryVitals += ",";
        logEntryVitals += tempError;
        logEntryVitals += ",";
        logEntryVitals += tempRESP;
        logEntryVitals += "\n";
        
        // DONE CREATING ONE BUFFER CYCLE
        // THIS Procedure will go and fill the buffers
        // 20 times before dumping them in the USB.
        
    }
    else{
        //Start Over
        logEntryVitals = NULL;
        logEntryTemp = NULL;
        logEntryOthers = NULL;
    } 
}

/* --------------------------------------------------------- */

void printUSBBUffer(void){
    
    // print the buffers for the CSV files
    
    // 1. open the file, 
    // 2. send the print command
    // 3. send the length 
    // 4. send data to be written
    // 5. close the file
    
    // ========== Temperature File =============
    
    //1.
    Serial2.print("OPW TempData");
    Serial2.print(SessionID);
    Serial2.print(".CSV");
    Serial2.print(13, BYTE);
    delay(100);
    
    //2.
    Serial2.print("WRF ");
    
    //3.
    Serial2.print(logEntryTemp.length());
    Serial2.print(13, BYTE);
    
    //4.
    Serial2.print(logEntryTemp);
    Serial2.print(13, BYTE);
    
    //6.
    Serial2.print("CLF TempData");
    Serial2.print(SessionID);
    Serial2.print(".CSV");
    Serial2.print(13, BYTE);
    delay(100);
    
    // ========== Vital Signs File =============
    
    //1.
    Serial2.print("OPW Vitals");
    Serial2.print(SessionID);
    Serial2.print(".CSV");
    Serial2.print(13, BYTE);
    delay(100);
    
    //2.
    Serial2.print("WRF ");
    
    //3.
    Serial2.print(logEntryVitals.length());
    Serial2.print(13, BYTE);
    
    //4.
    Serial2.print(logEntryVitals);
    Serial2.print(13, BYTE);
    
    //5.
    Serial2.print("CLF Vitals");
    Serial2.print(SessionID);
    Serial2.print(".CSV");
    Serial2.print(13, BYTE);
    delay(100);
    
    // ========== Other Parameters File =============
    
    //1.
    Serial2.print("OPW Others");
    Serial2.print(SessionID);
    Serial2.print(".CSV");
    Serial2.print(13, BYTE);
    delay(100);
    
    //2.
    Serial2.print("WRF ");
    
    //3.
    Serial2.print(logEntryOthers.length());
    Serial2.print(13, BYTE);
    
    //4.
    Serial2.print(logEntryOthers);
    Serial2.print(13, BYTE);
    
    //5.
    Serial2.print("CLF Others");
    Serial2.print(SessionID);
    Serial2.print(".CSV");
    Serial2.print(13, BYTE);
    delay(100);
    
}

/* --------------------------------------------------------- */
/* --------------------------------------------------------- */
/* --------------------------------------------------------- */


